package edu.washington.cs.workflow.matching;

import java.util.Collection;

import com.ibm.wala.ipa.callgraph.CGNode;

import edu.washington.cs.workflow.analysis.AnalysisBuilder;
import edu.washington.cs.workflow.analysis.AnalysisBuilder.CG;
import edu.washington.cs.workflow.analysis.AnalysisBuilder.UIFramework;
import edu.washington.cs.workflow.util.Globals;
import edu.washington.cs.workflow.util.Utils;
import junit.framework.TestCase;

public class TestGanttProject extends TestCase {
	
	public static String[] pkgs = new String[]{"net.sourceforge.ganttproject.",
			"org.bardsoftware.eclipsito",
			"org.ganttproject"};
	
	private static String corelib = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.0\\plugins\\net.sourceforge.ganttproject_2.0.0\\lib\\core\\";
	private static String oldpdflib = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.0\\plugins\\org.ganttproject.impex.htmlpdf_2.0.0\\lib\\";
	private static String oldMpxj = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.0\\plugins\\org.ganttproject.impex.msproject_2.0.0\\lib\\mpxj\\";
	public static String oldDir = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.0\\eclipsito.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.0\\plugins\\net.sourceforge.ganttproject_2.0.0\\ganttproject.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.0\\plugins\\org.ganttproject.chart.pert_2.0.0\\pert.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.0\\plugins\\org.ganttproject.impex.htmlpdf_2.0.0\\ganttproject-htmlpdf.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.0\\plugins\\org.ganttproject.impex.msproject_2.0.0\\ganttproject-msproject.jar"
	    + Globals.pathSep + corelib + "AppleJavaExtensions.jar"
	    + Globals.pathSep + corelib + "commons-httpclient-contrib.jar"
	    + Globals.pathSep + corelib + "commons-httpclient.jar"
	    + Globals.pathSep + corelib + "commons-logging.jar"
	    + Globals.pathSep + corelib + "commons-transaction-1.0.jar"
	    + Globals.pathSep + corelib + "jakarta-poi-2.5.jar"
	    + Globals.pathSep + corelib + "jakarta-slide-webdavlib-2.1.jar"
	    + Globals.pathSep + corelib + "jdnc-modifBen.jar"
	    + Globals.pathSep + corelib + "jdom-1.0.jar"
	    + Globals.pathSep + corelib + "jgoodies-looks-1.2.2.jar"
	    + Globals.pathSep + corelib + "xml-im-exporter1.1.jar"
	    + Globals.pathSep + oldpdflib + "fop-font-metrics.jar"
	    + Globals.pathSep + oldpdflib + "fop.jar"
	    + Globals.pathSep + oldpdflib + "ganttproject-avalon.jar"
	    + Globals.pathSep + oldpdflib + "ganttproject-batik.jar"
	    + Globals.pathSep + oldpdflib + "itext.jar"
	    + Globals.pathSep + oldMpxj + "jax-qname.jar"
	    + Globals.pathSep + oldMpxj + "jaxb-api.jar"
	    + Globals.pathSep + oldMpxj + "jaxb-impl.jar"
	    + Globals.pathSep + oldMpxj + "jaxb-libs.jar"
	    + Globals.pathSep + oldMpxj + "mpxj.jar"
	    + Globals.pathSep + oldMpxj + "namespace.jar"
	    + Globals.pathSep + oldMpxj + "relaxngDatatype.jar"
	    + Globals.pathSep + oldMpxj + "xsdlib.jar"
	    ;
	
	private static String newcorelib = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.5.4\\plugins\\net.sourceforge.ganttproject\\lib\\core\\";
	private static String newpdflib = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.5.4\\plugins\\org.ganttproject.impex.htmlpdf\\lib\\";
	private static String newprojectlib = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.5.4\\plugins\\biz.ganttproject.impex.msproject2\\lib\\";
	public static String newDir = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.5.4\\eclipsito.jar"
		+ Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.5.4\\plugins\\biz.ganttproject.impex.msproject2\\ganttproject-msproject2.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.5.4\\plugins\\net.sourceforge.ganttproject\\ganttproject.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.5.4\\plugins\\org.ganttproject.chart.pert\\pert.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\gantt2.5.4\\plugins\\org.ganttproject.impex.htmlpdf\\ganttproject-htmlpdf.jar"
	    + Globals.pathSep + newcorelib + "AppleJavaExtensions.jar"
	    + Globals.pathSep + newcorelib + "balloontip-1.2.1.jar"
	    + Globals.pathSep + newcorelib + "commons-httpclient.jar"
	    + Globals.pathSep + newcorelib + "commons-logging.jar"
	    + Globals.pathSep + newcorelib + "commons-net-ftpclient-3.0.1.jar"
	    + Globals.pathSep + newcorelib + "jakarta-slide-webdavlib-2.1.jar"
	    + Globals.pathSep + newcorelib + "jcommander-1.17.jar"
	    + Globals.pathSep + newcorelib + "jdnc-modifBen.jar"
	    + Globals.pathSep + newcorelib + "jdom-1.0.jar"
	    + Globals.pathSep + newcorelib + "jgoodies-common-1.1.1.jar"
	    + Globals.pathSep + newcorelib + "jgoodies-looks-2.4.0.jar"
	    + Globals.pathSep + newcorelib + "milton-api-2.0.3.jar"
	    + Globals.pathSep + newcorelib + "milton-client-2.0.3.jar"
	    + Globals.pathSep + newpdflib + "fop-font-metrics.jar"
	    + Globals.pathSep + newpdflib + "fop.jar"
	    + Globals.pathSep + newpdflib + "ganttproject-avalon.jar"
	    + Globals.pathSep + newpdflib + "ganttproject-batik.jar"
	    + Globals.pathSep + newpdflib + "itextpdf-5.1.3.jar"
	    + Globals.pathSep + newprojectlib + "mpxj.jar"
//	    + Globals.pathSep + newprojectlib + "mspdi_fix.xsl"
	    + Globals.pathSep + newprojectlib + "poi-3.7-20101029.jar"
		;
	
	public static String main = "Lorg/bardsoftware/eclipsito/Boot";

	public void testFindMethodInGanttProject() {
		AnalysisBuilder oldBuilder = new AnalysisBuilder(oldDir, main, pkgs);
		oldBuilder.setCGType(CG.RTA);
		oldBuilder.setUIFramework(UIFramework.SWING);
		oldBuilder.buildAnalysis();
		
		AnalysisBuilder newBuilder = new AnalysisBuilder(newDir, main, pkgs);
		newBuilder.setCGType(CG.RTA);
		newBuilder.setUIFramework(UIFramework.SWING);
		newBuilder.buildAnalysis();
		
		ExecutionTraceMatcher matcher = new ExecutionTraceMatcher(oldBuilder, newBuilder);
		matcher.setThreshold(0.9f);
//		for(CGNode node : oldBuilder.getCallGraph()) {
//			if(node.getMethod().getSignature().indexOf("freemind.modes.mindmapmode") != -1) {
//				System.out.println(node.getMethod().getSignature());
//			}
//		}
		
		//the 1st workflow
		String sig11 = "net.sourceforge.ganttproject.gui.zoom.ZoomManager.canZoomIn()Z";
		String sig12 = "net.sourceforge.ganttproject.action.ZoomInAction.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		
		//the 2nd workflow
		String sig21 = "net.sourceforge.ganttproject.gui.scrolling.ScrollingManagerImpl.scrollRight()V";
		String sig22 = "net.sourceforge.ganttproject.action.ScrollGanttChartRightAction.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		String sigNewScroll = "net.sourceforge.ganttproject.gui.scrolling.ScrollingManagerImpl.scrollBy(Lnet/sourceforge/ganttproject/task/TaskLength;)V";
		
		//the 3rd workflow
		
		//the 4th workflow
		String sig41 = "net.sourceforge.ganttproject.gui.GanttStatusBar.run()V";
		String sig42 = "net.sourceforge.ganttproject.action.CalculateCriticalPathAction.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		
		
		//the 5th workflow
		
		
		//the 6th workflow
		
		
		
		
		System.out.println("\nThe 1st workflow");
		CGNode node11 = oldBuilder.findCGNodeBySignature(sig11);
		CGNode node12 = oldBuilder.findCGNodeBySignature(sig12);
		System.out.println(node11);
		System.out.println(node12);
		Utils.checkNotNull(node11);
		Utils.checkNotNull(node12);
		CGNode matchedNode11 = matcher.findMatchedMethodByName(node11);
		CGNode matchedNode12 = matcher.findMatchedMethodByName(node12);
		System.out.println("matched 11: " + matchedNode11);
		System.out.println("matched 12: " + matchedNode12);
		Utils.checkNotNull(matchedNode11);
		Utils.checkTrue(matchedNode12 == null);
		CGNode sameNode12 = matcher.findSameMethodByName(node12);
		System.out.println("same 12: " + sameNode12);
		Utils.checkNotNull(sameNode12);
		CGNode counterpart11 = matcher.findCounterpart(node11);
		Utils.checkNotNull(counterpart11);
		CGNode counterpart12 = matcher.findCounterpart(node12);
		Utils.checkNotNull(counterpart12);
		
		System.out.println("\nThe 2nd workflow");
		CGNode node21 = oldBuilder.findCGNodeBySignature(sig21);
		CGNode node22 = oldBuilder.findCGNodeBySignature(sig22);
		System.out.println(node21);
		System.out.println(node22);
		Utils.checkNotNull(node21);
		Utils.checkNotNull(node22);
		CGNode matchedNode21 = matcher.findMatchedMethodByName(node21);
		CGNode matchedNode22 = matcher.findMatchedMethodByName(node22);
		CGNode sameNode21 = matcher.findSameMethodByName(node21);
		CGNode sameNode22 = matcher.findSameMethodByName(node22);
		Utils.checkTrue(matchedNode21 == null);
		Utils.checkTrue(matchedNode22 == null);
		Utils.checkTrue(sameNode21 == null);
		Utils.checkTrue(sameNode22 == null);
		matcher.setSimilarNumber(20);
		Collection<CGNode> similar21 = matcher.findAllSimilarMethodsByFullName(node21);
		for(CGNode similar : similar21) {
			System.out.println("--" + similar.getMethod().getSignature());
		}
//		net.sourceforge.ganttproject.action.scroll.ScrollTimeIntervalAction.actionPerformed
//		net.sourceforge.ganttproject.action.scroll.ScrollTimeIntervalAction.createTimeInterval()
//		net.sourceforge.ganttproject.gui.scrolling.ScrollingManagerImpl.scrollBy
		Collection<CGNode> similar22 = matcher.findAllSimilarMethodsByFullName(node22);
		for(CGNode similar : similar22) {
			System.out.println("++" + similar.getMethod().getSignature());
		}
		Utils.checkNotNull(newBuilder.findCGNodeBySignature(sigNewScroll));
		CGNode counterpart21 = matcher.findCounterpart(node21);
		Utils.checkNotNull(counterpart21);
		
		System.out.println("\nThe 3rd workflow");
		
		System.out.println("\nThe 4th workflow");
		CGNode node42 = oldBuilder.findCGNodeBySignature(sig42);
		System.out.println(node42);
		Utils.checkNotNull(node42);
		CGNode matchedNode42 = matcher.findMatchedMethodByName(node42);
		System.out.println("matched 42: " + matchedNode42);
		Utils.checkNotNull(matchedNode42);
		CGNode counterpart42 = matcher.findCounterpart(node42);
		Utils.checkNotNull(counterpart42);
		
		System.out.println("\nThe 5th workflow");
		
		System.out.println("\nThe 6th workflow");
	}
//	private void show(AnalysisBuilder builder, String sig) {
//	    for(CGNode node : builder.getCallGraph()) {
//		    if(node.getMethod().getSignature().indexOf(sig) != -1) {
//			    System.out.println("--" + node.getMethod().getSignature());
//		    }
//	    }
//	}
}
